<template>
  <div class="login">
    <div class="login-box">
      <img
        src="@/assets/login/breakleft.jpg"
        alt=""
      >
      <div class="login-form">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
          <div class="login-form-title">
            <img
              src="@/assets/login/on_logo.png"
              style="width: 500px; height:80px"
              alt=""
            >
          </div>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="请输入账号"
              prefix-icon="iconfont icon-user"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="iconfont icon-lock"
              @keyup.enter.native="handleLogin"
            />
          </el-form-item>
          <el-form-item style="width: 100%">
            <el-button
              type="primary"
              style="width: 100%; height: 50px;"
              @click.native.prevent="handleLogin
              "class="login-btn"
              size="medium"
            >
              <span v-if="!loading" style="color: black;">登录</span>
              <span v-else>登录中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Watch } from 'vue-property-decorator'
import { Route } from 'vue-router/types/router';
import { Form as ElForm, Input } from 'element-ui'
import { UserModule } from '@/store/modules/user'
//import { isValidUsername } from '@/utils/validate'

@Component({
  name: 'Login',
})
export default class extends Vue {
  private validateUsername = (rule: any, value: string, callback: Function) => {
    if (!value) {
      callback(new Error('请输入用户名'))
    } else {
      callback()
    }
  }
  private validatePassword = (rule: any, value: string, callback: Function) => {
    if (value.length < 5) {
      callback(new Error('请输入5位以上的密码'))
    } else {
      callback()
    }
  }
  private loginForm = {
    username: 'admin',
    password: '123456',
  } as {
    username: String
    password: String
  }

  loginRules = {
    username: [{ validator: this.validateUsername, trigger: 'blur' }],
    password: [{ validator: this.validatePassword, trigger: 'blur' }],
  }
  private loading = false
  private redirect?: string

  @Watch('$route', { immediate: true })
  private onRouteChange(route: Route) {}

  // 登录
  private handleLogin() {
    //对输入信息进行校验
    (this.$refs.loginForm as ElForm).validate(async (valid: boolean) => {
      //校验通过
      if (valid) {
        this.loading = true
        await UserModule.Login(this.loginForm as any)
          .then((res: any) => {
            if (String(res.code) === '1') {
              //后端响应数据中code为1，则前端成功接收到后端的账号密码数据（成功），则跳转到'/'
              this.$router.push('/')
            } else {
              //this.$message.error('用户名或密码错误！')
              //后端响应数据中code为0，状态修改为未登录状态
              this.loading = false
            }
          })
          .catch(() => {
            //无法成功接收到后端数据，状态修改为为登录状态
            //this.$message.error('用户名或密码错误！')
            this.loading = false
          })
      } else {
        //校验不通过
        return false
      }
    })
  }
}
</script>

<style lang="scss">
.login {
  background: rgb(239, 253, 239);
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

.login-box {
  width: 1050px;
  height: 600px;
  border-radius: 8px;
  display: flex;
  img {
    width: 50%;
    height: auto;
  }
}

.title {
  margin: 0px auto 10px auto;
  text-align: left;
  color: #707070;
}

.login-form {
  background: #ffffff;
  width: 50%;
  border-radius: 0px 8px 8px 0px;
  display: flex;
  justify-content: center;
  align-items: center;
  .el-form {
    width: 214px;
    height: 264px;
  }
  .el-form-item {
    margin-bottom: 50px;
  }
  .el-form-item.is-error .el-input__inner {
    border: 0 !important;
    border-bottom: 1px solid #fd7065 !important;
    background: #fff !important;
  }
  .input-icon {
    height: 50px;
    width: 20px;
    margin-left: -2px;
  }
  .el-input__inner {
    border: 0;
    border-bottom: 1px solid #d2d2d1;
    border-radius: 0;
    font-size: 20px;
    font-weight: 350;
    color: #333333;
    height: 40px;
    line-height: 32px;
  }
  .el-input__prefix {
    left: 0;
  }
  .el-input--prefix .el-input__inner {
    padding-left: 26px;
  }
  .el-input__inner::placeholder {
    color: #aeb5c4;
  }
  .el-form-item--medium .el-form-item__content {
    line-height: 32px;
  }
  .el-input--medium .el-input__icon {
    line-height: 32px;
  }
}

.login-btn {
  border-radius: 5px;
  padding: 10px 20px !important;
  margin-top: 10px;
  font-weight: 250;
  font-size: 15px;
  border: 0;
  color: #333333;
  background: #9ce7d1;
  &:hover,
  &:focus {
    background: #a5eed9;
    color: #d3d3d3;
  }
}
.login-form-title {
  height: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 40px;
  .title-label {
    font-weight: 500;
    font-size: 20px;
    color: #333333;
    margin-left: 10px;
  }
}
</style>
